package com.pl.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pl.entity.SysUser;
import com.pl.entity.WmWarehouse;
import com.pl.lister.WmWarehouseReadLister;
import com.pl.service.WmWarehouseService;
import com.pl.utils.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 仓库表(WmWarehouse)表控制层
 *
 * @author makejava
 * @since 2024-04-26 18:46:03
 */
@RestController
@RequestMapping("/api/wmWarehouse")
public class WmWarehouseController {
    /**
     * 服务对象
     */
    @Autowired
    private WmWarehouseService wmWarehouseService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @RequestMapping("/list/page")
    public R queryByPage(@RequestParam Map<String, Object> map) {
        int pageNum = Integer.valueOf((String) map.get("pageNum"));
        int pageSize = Integer.valueOf((String) map.get("pageSize"));
        PageHelper.startPage(pageNum, pageSize);
        List<WmWarehouse> wmWarehouses = this.wmWarehouseService.queryByPage(map);
        if (wmWarehouses != null) {
            PageInfo<WmWarehouse> warehousePageInfo = new PageInfo<>(wmWarehouses);
            return R.ok(warehousePageInfo);
        }
        return R.fail("查询失败");
    }

    /**
     * 新增数据
     *
     * @param wmWarehouse 实体
     * @return 新增结果
     */
    @RequestMapping("/save")
    public R add(WmWarehouse wmWarehouse, HttpSession session) {
        boolean insert = wmWarehouseService.insert(wmWarehouse,session);
        if (insert) {
            return R.ok();
        }
        return R.fail("插入失败");
    }

    /**
     * 编辑数据
     *
     * @param wmWarehouse 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    public R edit(WmWarehouse wmWarehouse) {
        boolean update = this.wmWarehouseService.update(wmWarehouse);
        if (update) {
            return R.ok();
        }
        return R.fail("修改失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("/del/{id}")
    public R deleteById(@PathVariable("id") Long id) {
        boolean result = this.wmWarehouseService.deleteById(id);
        if (result) {
            return R.ok();
        }
        return R.fail("删除失败");
    }

    /**
     * 批量删除
     *
     * @return list
     */
    @RequestMapping("/del/batch")
    public R deleteBatch(@RequestParam List<Integer> list) {
        boolean result = wmWarehouseService.deleteBatch(list);
        if (result) {
            return R.ok();
        }
        return R.fail("批量插入失败");
    }

    /**
     * 上传文件（需要监听器，批量增加）
     * @param file
     * @return
     */
    @RequestMapping("/importwmWarehouseFile")
    public String importwmWarehouseFile(@Param("file") MultipartFile file){
        return wmWarehouseService.importwmWarehouseFile(file);
    }

    /**
     * 导出文件
     * @return
     */
    @RequestMapping("/exportwmWarehouseFile")
    public void exportwmWarehouseFile(HttpServletResponse response){
        wmWarehouseService.exportwmWarehouseFile(response);
    }

    /**
     * 查找全部数据
     * @return
     */
    public R finAll(){
        boolean result=wmWarehouseService.finAll();
        if (result){
            return R.ok();
        }
        return R.fail("查找失败");
    }

}

